﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Ink;

namespace TileEditorControls
{
    /// <summary>
    /// Interaction logic for TileEditorGrid.xaml
    /// </summary>
    public partial class TileEditorGrid : UserControl
    {
        private static DependencyProperty RowsProperty =
            DependencyProperty.Register("Rows", typeof(int), typeof(TileEditorGrid),
                                        new PropertyMetadata(5,OnRowsPropertyChanged));

        private static DependencyProperty ColsProperty =
            DependencyProperty.Register("Cols", typeof (int), typeof (TileEditorGrid),
                                        new PropertyMetadata(5, OnColsPropertyChanged));

        private static DependencyProperty CurrentCellProperty =
            DependencyProperty.Register("CurrentCell", typeof (Point), typeof (TileEditorGrid)
                                                , new PropertyMetadata());

        private static DependencyProperty CurrentBrushProperty = 
            DependencyProperty.Register("CurrentBrush", typeof(Brush), typeof(TileEditorGrid)
                                                , new PropertyMetadata());

        private int CELLWIDTH = 25;
        private int CELLHEIGHT = 25;

        private List<List<Tile>> cells;

        
        /// <summary>
        /// The Number of Columns in this TileEditor
        /// </summary>
        public int Cols
        {
            get { return (int) GetValue(ColsProperty); }
            set { SetValue(ColsProperty,value);  }
        }

        /// <summary>
        /// The Number of Rows in this TileEditor
        /// </summary>
        public int Rows
        {
            get { return (int)GetValue(RowsProperty); }
            set { SetValue(RowsProperty,value); }
        }

        /// <summary>
        /// The Currently Selected Cell
        /// </summary>
        public Point CurrentCell
        {
            get { return (Point)GetValue(CurrentCellProperty); }
            set { SetValue(CurrentCellProperty,value); }
        }

        /// <summary>
        /// The Currently Selected Cell
        /// </summary>
        public Brush CurrentBrush
        {
            get { return (Brush)GetValue(CurrentBrushProperty); }
            set { SetValue(CurrentBrushProperty, value); }
        }

        public int PointerSize { get; set; }
        

        public TileEditorGrid()
        {
            InitializeComponent();
            cells = new List<List<Tile>>();
            PointerSize = 1;
        }

        /// <summary>
        /// Fills the grid with cells, based on Cols and Rows properties
        /// </summary>
        /// <param name="rows"></param>
        /// <param name="cols"></param>
        private void DrawGrid( int rows, int cols)
        {
            BaseGrid.Children.Clear();
            BaseGrid.RowDefinitions.Clear();
            BaseGrid.ColumnDefinitions.Clear();
            BaseGrid.Background = Brushes.Black;

            for (int x = 0; x < cols; x++)
            {
                BaseGrid.RowDefinitions.Add(new RowDefinition());
                BaseGrid.ColumnDefinitions.Add(new ColumnDefinition());
                for (int y = 0; y < rows; y++)
                {
                    Tile tile = new Tile();
                    tile.BackGround = new SolidColorBrush(Colors.Black);
                    tile.BorderBrush = new SolidColorBrush(Colors.Gray);
                    tile.BorderThickness = new Thickness(1);
                    tile.MouseLeftButtonDown += TileOnClick;
                    tile.MouseRightButtonDown += TileOnClick;
                    tile.Width = CELLWIDTH;
                    tile.Height = CELLHEIGHT;
                    tile.TileHover += new TileHoverRoutedEventHandler(tile_TileHover);
                    BaseGrid.Children.Add(tile);
                    Grid.SetRow(tile, y);
                    Grid.SetColumn(tile, x);
                }

                BaseGrid.Width = Cols * CELLWIDTH;
                BaseGrid.Height = Rows * CELLHEIGHT;  
            }
        }

        void tile_TileHover(object sender, TileHoverEventArgs e)
        {
            int size = PointerSize;

            if (CurrentBrush != null)
            {
                SelectionOverlay.Width = CELLWIDTH * size;
                SelectionOverlay.Height = CELLHEIGHT * size;
                SelectionOverlay.Fill = null;
                SelectionOverlay.Stroke = Brushes.Red;
                SelectionOverlay.StrokeThickness = 2;
            }

            Canvas.SetTop( SelectionOverlay, e.yPos * CELLHEIGHT );
            Canvas.SetLeft(SelectionOverlay, e.xPos * CELLWIDTH);
            
        }

        private void TileOnClick( object sender,  MouseButtonEventArgs args)
        {
            // if the tile is clicked we have to fill it with the currentbrush
            // we also have to fill adjacent cells if the pointersize is bigger than 1

            Tile curTile = sender as Tile;
            if (curTile != null && CurrentBrush != null)
            {
                CurrentCell = new Point(Grid.GetColumn(curTile), Grid.GetRow(curTile));
                curTile.BackGround = CurrentBrush;
                curTile.BorderThickness = new Thickness(0);
            }

            if (args.ChangedButton == MouseButton.Right)
            {
                curTile.BackGround = Brushes.Transparent;
                curTile.BorderBrush = Brushes.Gray;
                curTile.BorderThickness = new Thickness(1);
            }
        }

        private static void OnColsPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            TileEditorGrid tGrid = (TileEditorGrid)sender;
            tGrid.DrawGrid(tGrid.Rows, (int)args.NewValue);
        }

        private static void OnRowsPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            TileEditorGrid tGrid = (TileEditorGrid)sender;
            tGrid.DrawGrid((int)args.NewValue,tGrid.Cols);
        }

        private void SelectionOverlay_MouseDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = false;
            
        }


   }
}
